//
//  FIGoodsDetailsViewController.m
//  cherryproduct
//
//  Created by zou shigang on 14/9/6.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "FIGoodsDetailsViewController.h"
#import "FIGoodsDetailsCell.h"
#import "FIGoodsDetailsHeader.h"
#import "BuyProductViewController.h"
#import "UMSocial.h"
#import "SGBuyPageViewController.h"

@interface FIGoodsDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate, UMSocialUIDelegate>
{
    UIButton * phoneButton_bb;
    
    UIImageView * image_share_0;
}

@property(nonatomic, strong) UICollectionView * collection_view;
@property(nonatomic, strong) UIView * view_bar_bg;
@property(nonatomic, strong) UILabel * label_title;
@property(nonatomic, strong) UILabel * label_time;

@property(nonatomic, strong) NSMutableArray * list_imgs;
@property(nonatomic, strong) NSMutableArray * list_goods;
@property(nonatomic, strong) NSMutableString * data_net_des;

@property(nonatomic, strong) NSTimer * time_out;
@property(nonatomic, assign) double end_time;

@property(nonatomic, assign) double end_time_change;

@end

@implementation FIGoodsDetailsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    if (self.time_out) {
        if([self.time_out isValid]) {
            [self.time_out invalidate];
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.list_imgs = [[NSMutableArray alloc] initWithCapacity:5];
    self.list_goods = [[NSMutableArray alloc] initWithCapacity:5];
    // Do any additional setup after loading the view.
    self.myView.hidden = YES;
    [self setNavigationBarColor:[UIColor clearColor]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (self.collection_view == nil) {
        UICollectionViewFlowLayout * layout= [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        [layout setItemSize:CGSizeMake(155, 180)]; //设置每个cell显示数据的宽和高必须
        layout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 0);
        layout.minimumLineSpacing=1;
        layout.minimumInteritemSpacing=1;
        layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 294);
        
        CGFloat f_height = 20;
        if ([[UIDevice currentDevice].systemVersion doubleValue] < 7.0) {
            f_height=0;
        }
        self.collection_view = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
        self.collection_view.backgroundColor=[UIColor whiteColor];
        self.collection_view.delegate=self;
        self.collection_view.dataSource=self;
        self.collection_view.bounces=YES;
        self.collection_view.showsHorizontalScrollIndicator=NO;
        self.collection_view.showsVerticalScrollIndicator=NO;
        [self.collection_view registerClass:[FIGoodsDetailsCell class] forCellWithReuseIdentifier:@"FIGoodsDetailsCell"];
        [self.collection_view registerClass:[FIGoodsDetailsHeader class]
                 forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                        withReuseIdentifier:@"FIGoodsDetailsHeader"];
        
        [self.view addSubview:self.collection_view];
    }
    
    if (self.view_bar_bg == nil) {
        CGFloat width = self.view.frame.size.width;
        self.view_bar_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 64)];
        if (self.str_color_bg && [self.str_color_bg isKindOfClass:[NSString class]] && ![self.str_color_bg isEqual:@""]) {
            self.view_bar_bg.backgroundColor=[FIConstMethod colorWithHexString:self.str_color_bg withAlpha:0.6];
        }
        [self.view addSubview:self.view_bar_bg];
        
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 44)];
        if (IOS7_OR_LATER) {
            [backButton setImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateNormal];
            [backButton setImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateHighlighted];
        } else {
            [backButton setBackgroundImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateNormal];
            [backButton setBackgroundImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateHighlighted];
        }
        [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view_bar_bg addSubview:backButton];
        
        self.label_title = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, width-100, 44)];
        self.label_title.textColor = [UIColor whiteColor];
        self.label_title.backgroundColor = [UIColor clearColor];
        self.label_title.font = [UIFont systemFontOfSize:20];
        self.label_title.textAlignment = NSTextAlignmentCenter;
        self.label_title.text = @"团购详情";
        [self.view_bar_bg addSubview:self.label_title];
        
        self.label_time = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, width-100, 14)];
        self.label_time.textColor = [UIColor blackColor];
        self.label_time.backgroundColor = [UIColor clearColor];
        self.label_time.font = [UIFont systemFontOfSize:9];
        self.label_time.textAlignment = NSTextAlignmentCenter;
        self.label_time.text = @"本次团购剩余时间:0:0:0";
        [self.view_bar_bg addSubview:self.label_time];
        
        if (self.m_b_is_notice) {
            //预告的团购详情:显示订阅按钮
            phoneButton_bb = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_BOUNDS_WIDTH-84, IOS7_OR_LATER?20:0, [UIImage imageNamed:@"upfuturn"].size.width-0, 44)];
            [phoneButton_bb setImage:[UIImage imageNamed:@"downupfuturn"] forState:UIControlStateNormal];
            [phoneButton_bb setImage:[UIImage imageNamed:@"upfuturn"] forState:UIControlStateSelected];
            [phoneButton_bb addTarget:self action:@selector(doActionNotice:) forControlEvents:UIControlEventTouchUpInside];
            [self.view_bar_bg addSubview:phoneButton_bb];
            
            if (self.m_b_is_have_toto) {
                phoneButton_bb.selected = YES;
            } else {
                phoneButton_bb.selected = NO;
            }
            
            UIButton * phoneButton = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_BOUNDS_WIDTH-54, IOS7_OR_LATER?20:0, [UIImage imageNamed:@"share.png"].size.width-0, 44)];
            [phoneButton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
            [phoneButton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateHighlighted];
            [phoneButton addTarget:self action:@selector(doActionShareInfo:) forControlEvents:UIControlEventTouchUpInside];
            [self.view_bar_bg addSubview:phoneButton];
        } else {
            UIButton * phoneButton = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_BOUNDS_WIDTH-54, IOS7_OR_LATER?20:0, [UIImage imageNamed:@"share.png"].size.width-0, 44)];
            [phoneButton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
            [phoneButton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateHighlighted];
            [phoneButton addTarget:self action:@selector(doActionShareInfo:) forControlEvents:UIControlEventTouchUpInside];
            [self.view_bar_bg addSubview:phoneButton];
        }
    }
    
    [self getData];
}

- (void)doActionShareInfo:(id)sender
{
    //NSString * str_url_share_tuan = [NSString stringWithFormat:@"http://180.150.179.226/cherry/api.php/Groupon/groupon_buy_detail/id/%@", self.str_goods_id];
    NSString * str_url_share_tuan = [NSString stringWithFormat:@"http://cherry8.com/api.php/Groupon/groupon_buy_detail/id/%@", self.str_goods_id];
    
    NSString * str_send_info = [NSString stringWithFormat:@"樱桃优品--%@  %@ ", self.label_title.text, str_url_share_tuan];
    //@"http://www.weibo.com/2029912903/B3ogzA3p6"
    
    NSString * str_send_share_weixin = [NSString stringWithFormat:@"樱桃优品--%@", self.label_title.text];
    //樱桃优品  用心为您选择
//    [UMSocialWechatHandler setWXAppId:@"wx42bae2a079770c21"
//                            appSecret:@"85762a1a96144f320249c0e3416074fe"
//                                  url:str_send_info];
    
    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeApp;
    
    [UMSocialData defaultData].extConfig.title = str_send_share_weixin;//self.label_title.text;
    //[UMSocialData defaultData].extConfig.wechatSessionData.url = self.str_share_url_address;
    [UMSocialData defaultData].extConfig.wxMessageType=UMSocialWXMessageTypeWeb;
    [UMSocialData defaultData].extConfig.wechatSessionData.url=str_url_share_tuan;
    [UMSocialData defaultData].extConfig.wechatSessionData.shareText=[NSString stringWithFormat:@"樱桃优品 用心为您选择%@", self.data_net_des];
    
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = str_url_share_tuan;//@"http://www.taobao.com";
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"53fc9df6fd98c5c9a0009069"
                                      shareText:str_send_info
                                     shareImage:image_share_0.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToWechatSession, UMShareToWechatTimeline, nil]
                                       delegate:self];
}
//实现回调方法（可选）：
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}


- (void)doActionNotice:(id)sender
{
    //...
    NSString * loginInfo = [[LoginViewController shareUserInfo] objectForKey:KEY_HAVE_LOGIN];
    
    if (loginInfo && [loginInfo isKindOfClass:[NSString class]] && [loginInfo isEqualToString:@"1"]){
        
        if (!self.m_b_is_have_toto) {//添加提醒
            if (self.str_goods_id && [self.str_goods_id isKindOfClass:[NSString class]]) {
                NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
                [dic setObject:self.str_goods_id forKey:@"id"];
                //添加团购提醒
                [[HttpNewBaseService sharedClient] getRequestOperation:@"groupon/alarm_join" paramDictionary:dic onCompletion:^(NSString *responseString) {
                    NSDictionary * jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                    if([[jsondic objectForKey:@"status"] intValue]==1)//成功
                    {
                        //[self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                        self.m_b_is_have_toto=YES;
                        if (self.m_b_is_have_toto) {
                            phoneButton_bb.selected = YES;
                        } else {
                            phoneButton_bb.selected = NO;
                        }

                    } else {
                        [self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                    }
                } onError:^(NSError *error) {
                    [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
                } onAnimated:YES];
            }//...
        } else {//去掉提醒
            NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
            [dic setObject:self.str_goods_id forKey:@"id"];
            [[HttpNewBaseService sharedClient] getRequestOperation:@"groupon/alarm_cancel" paramDictionary:dic onCompletion:^(NSString *responseString) {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                if ([[jsondic objectForKey:@"status"] intValue]==1)//成功
                {
                    //[self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                    self.m_b_is_have_toto=NO;
                    if (self.m_b_is_have_toto) {
                        phoneButton_bb.selected = YES;
                    } else {
                        phoneButton_bb.selected = NO;
                    }
                    
                } else {
                    [self showAlertView:@"温馨提示" subtitle:[jsondic objectForKey:@"message"]];
                }
                
            } onError:^(NSError *error) {
                [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
            } onAnimated:YES];
        }
        
        
    } else {
        LoginViewController * vc = [[LoginViewController alloc] init];
        vc.m_is_come_other=YES;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData
{
    NSMutableDictionary * dic_pra = [[NSMutableDictionary alloc] init];
    [dic_pra setValue:self.str_goods_id forKeyPath:@"id"];
    
    [self.view startLoadingAnimation];
    
    [[HttpNewBaseService sharedClient] GET:@"Groupon/groupon_detail" parameters:dic_pra success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [self.view stopLoadingAnimation];
        
        NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
        if (status==1) {
            
            NSDictionary * data = [responseObject objectForKey:@"data"];
            if (data && [data isKindOfClass:[NSDictionary class]]) {
                
                NSDictionary * groupon = [data objectForKey:@"groupon"];
                if (groupon && [groupon isKindOfClass:[NSDictionary class]]) {
                    NSArray * array_photo = [groupon objectForKey:@"groupon_photo"];
                    if (array_photo && [array_photo isKindOfClass:[NSArray class]]) {
                        [self.list_imgs addObjectsFromArray:array_photo];
                    }
                    
                    NSString * str_des = [groupon objectForKey:@"des"];
                    if (str_des && [str_des isKindOfClass:[NSString class]]) {
                        self.data_net_des= [NSMutableString stringWithFormat:@"        %@", str_des];
                        /*
                        if (self.data_net_des.length<140) {
                            NSInteger count = 140-self.data_net_des.length;
                            for (int i = 0; i < count/4+1; i++) {
                                [self.data_net_des appendFormat:@"不够字呢"];
                            }
                        }*/
                    }
                    
                    NSString * str_title = [groupon objectForKey:@"title"];
                    if (str_title && [str_title isKindOfClass:[NSString class]]) {
                        self.label_title.text=str_title;
                    }
                    //获得时候开团
                    //判断是否开团
                    {
                        double str_start_time_formate = [[groupon objectForKey:@"start_time"] doubleValue];
                        NSDate * destDate = [NSDate dateWithTimeIntervalSince1970:str_start_time_formate];
                        
                        //NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                        //[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
                        //NSDate * destDate= [dateFormatter dateFromString:str_start_time_formate];
                        
                        NSDate * date_now = [NSDate date];
                        
                        if ([date_now compare:destDate] == NSOrderedAscending) {
                            {
                                //self.m_b_is_notice=YES;
                            }
                        }
                    }
                    
                    //得到结束时间
                    if (self.m_b_is_notice) {
                        self.end_time = [[groupon objectForKey:@"start_time"] doubleValue];
                        self.end_time_change = [[groupon objectForKey:@"end_time"] doubleValue];
                    } else {
                        self.end_time = [[groupon objectForKey:@"end_time"] doubleValue];
                    }
                    
                    [self getStartAction];
                }
                
                NSArray * goods = [data objectForKey:@"goods"];
                if (goods && [goods isKindOfClass:[NSArray class]]) {
                    [self.list_goods addObjectsFromArray:goods];
                }
            }
            
            [self.collection_view reloadData];
        } else {
            [self showAlertView:@"温馨提示" subtitle:@"服务器参数错误!"];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    }];
}

- (void)getStartAction
{
    double timetemp = self.end_time - [[NSDate date] timeIntervalSince1970];
    NSDateFormatter * fortimemat = [[NSDateFormatter alloc] init];
    [fortimemat setDateFormat:@"HH:mm:ss"];
    
    NSTimeInterval time = timetemp;
    int days=(time)/(3600*24);
    int doutime = time - days*(3600*24);
    int hours=doutime/3600;
    int mont = (doutime%3600)/60;
    int sec = (doutime%3600)%60;
    
    NSString * dateContent = [[NSString alloc] initWithFormat:@"%d:%d:%d", days*24+hours, mont, sec];
    if (sec < 10) {
        dateContent = [[NSString alloc] initWithFormat:@"%d:%d:0%d", days*24+hours, mont, sec];
    }
    
    if (timetemp > 0) {
        //NSString * str_title_info = [fortimemat stringFromDate:[NSDate dateWithTimeIntervalSince1970:timetemp]];
        if (self.m_b_is_notice) {
            self.label_time.text = [NSString stringWithFormat:@"团购开始时间:%@", dateContent];
        } else {
            self.label_time.text = [NSString stringWithFormat:@"团购剩余时间:%@", dateContent];
        }

        self.time_out = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                         target:self
                                                       selector:@selector(timeoverdown:)
                                                       userInfo:nil
                                                        repeats:YES];
    } else {
        if (self.m_b_is_notice) {
            self.label_time.text = @"团购已经开始";
            
        } else {
            self.label_time.text = @"团购已经结束";
        }
    }
}

- (void)timeoverdown:(NSTimer*)timertemp //倒计时
{
    double timetemp = self.end_time - [[NSDate date] timeIntervalSince1970];
    NSDateFormatter * fortimemat = [[NSDateFormatter alloc] init];
    [fortimemat setDateFormat:@"HH:mm:ss"];
    
    NSTimeInterval time = timetemp;
    int days=(time)/(3600*24);
    int doutime = time - days*(3600*24);
    int hours=doutime/3600;
    int mont = (doutime%3600)/60;
    int sec = (doutime%3600)%60;
    
    NSString * dateContent = [[NSString alloc] initWithFormat:@"%d:%d:%d", days*24+hours, mont, sec];
    if (sec < 10) {
        dateContent = [[NSString alloc] initWithFormat:@"%d:%d:0%d", days*24+hours, mont, sec];
    }
    
    //labeltime.text = dateContent;
    
    if(timetemp>0) {
        //NSString * str_title_info = [fortimemat stringFromDate:[NSDate dateWithTimeIntervalSince1970:timetemp]];
        if (self.m_b_is_notice) {
            self.label_time.text = [NSString stringWithFormat:@"团购开始时间:%@", dateContent];
        } else {
            self.label_time.text = [NSString stringWithFormat:@"团购剩余时间:%@", dateContent];
        }
    } else {
        if (self.m_b_is_notice) {
            //self.label_time.text = @"团购已经开始";
            //变换按钮
            self.end_time=self.end_time_change;
            self.m_b_is_notice=NO;
            [self.collection_view reloadData];
            
        } else {
            self.label_time.text = @"团购已经结束";
        }
        [self.time_out invalidate];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark -
#pragma mark UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

- (void)doActionSenderGo:(NSString*)go_id
{
    {
        NSString * loginInfo = [[LoginViewController shareUserInfo] objectForKey:KEY_HAVE_LOGIN];
        
        if (loginInfo && [loginInfo isKindOfClass:[NSString class]] && [loginInfo isEqualToString:@"1"])
        {
            
        }
        else
        {
            LoginViewController * vc = [[LoginViewController alloc] init];
            vc.m_is_come_other=YES;
            [self.navigationController pushViewController:vc animated:YES];
            
            return;
        }
    }
    
    [self.view startLoadingAnimation];
    
    NSMutableDictionary * requestDic = [[NSMutableDictionary alloc] initWithCapacity:2];
    [requestDic setObject:go_id forKey:@"id"];
    
    [[HttpNewBaseService sharedClient] getRequestOperation:@"groupon/groupon_buy_log" paramDictionary:requestDic onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        
        NSDictionary * dic = [responseString JSONValue];
        if (dic) {
            NSInteger status = [[dic objectForKey:@"status"] integerValue];
            if (status == 1) {
                //跳转
                NSDictionary * data = [dic objectForKey:@"data"];
                if (data && [data isKindOfClass:[NSDictionary class]]) {
                    NSInteger soldout = [[data objectForKey:@"soldout"] integerValue];
                    if (soldout != 0) {//1完了,0有呢
                        [self showAlertView:@"温馨提示" subtitle:@"抱歉,商品卖光了,等下次吧!"];
                    } else {
                    
                        NSString * str_url = [data objectForKey:@"url"];
                        NSURL * url = nil;
                        if (str_url && [str_url isKindOfClass:[NSString class]]) {
                            url = [[NSURL alloc] initWithString:str_url];
                        } else {
                            url = [[NSURL alloc] initWithString:@"http://www.taobao.com"];
                            str_url=@"http://www.taobao.com";
                        }
                        //[[UIApplication sharedApplication]openURL:url];
                        SGBuyPageViewController * vc = [[SGBuyPageViewController alloc] init];
                        vc.str_url_page=str_url;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }
            } else {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        }
        
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        
    } onAnimated:YES];
}

- (void)doActionPut:(UIButton *)sender
{
    NSInteger index = sender.tag;
    if (index < [self.list_goods count]) {
        NSDictionary * dic = [self.list_goods objectAtIndex:index];
        
        if (dic && [dic isKindOfClass:[NSDictionary class]]) {
            /*
            BuyProductViewController * buy = [[BuyProductViewController alloc] init];
            NSString * buy_url = [dic objectForKey:@"buy_url"];
            if (YES) {
                buy_url = @"http://www.baidu.com";
            }
            buy.stringurl = buy_url;
            buy.stringid = [dic objectForKey:@"gid"];
            NSLog(@"%@,%@",buy.stringid, buy.stringurl);
            [self.navigationController pushViewController:buy animated:YES];
             */
            NSString * str_go_id = [dic objectForKey:@"gid"];
            [self doActionSenderGo:str_go_id];
        }
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    if (self.m_b_is_notice) {
        
        NSInteger index = indexPath.row;
        if (index < [self.list_goods count]) {
            NSDictionary * dic = [self.list_goods objectAtIndex:index];
            
            if (dic && [dic isKindOfClass:[NSDictionary class]]) {
                BuyProductViewController* buy = [[BuyProductViewController alloc] init];
                buy.stringurl =@"http://www.taobao.com";
                buy.stringid = [dic objectForKey:@"gid"];
                
                buy.m_b_is_come_s=NO;
                buy.m_b_is_have_say = [[dic objectForKey:@"is_commented"] boolValue];
                
                //share
                //buy.str_share_img_url = [NSString stringWithFormat:@"%@%@",JPG, [dic objectForKey:@"photo"]];
                buy.str_share_name = [[dic objectForKey:@"goods"] objectForKey:@"name"];
                FIGoodsDetailsCell * cell = (FIGoodsDetailsCell *)[collectionView cellForItemAtIndexPath:indexPath];
                if (cell) {
                    buy.image_share_ = cell.image_goods.image;
                }
                
                [self.navigationController pushViewController:buy animated:YES];
            }
        }
        
        return;
    }
    
    NSInteger index = indexPath.row;
    if (index < [self.list_goods count]) {
        NSDictionary * dic = [self.list_goods objectAtIndex:index];
        
        if (dic && [dic isKindOfClass:[NSDictionary class]]) {
            BuyProductViewController* buy = [[BuyProductViewController alloc] init];
            NSString * buy_url = [dic objectForKey:@"buy_url"];
            if (buy_url == nil || [buy_url rangeOfString:@"http://" options:NSCaseInsensitiveSearch].length <= 0) {
                buy_url = @"http://www.taobao.com";
            }
            buy.stringurl =buy_url;
            buy.stringid = [dic objectForKey:@"gid"];
            buy.m_b_is_come_s=YES;
            buy.str_money=@"0";
            /*
            if ([dic objectForKey:@"goods"] && [[dic objectForKey:@"goods"] isKindOfClass:[NSDictionary class]]) {
                NSDictionary * ddddic = [dic objectForKey:@"goods"];
                buy.str_money=[ddddic objectForKey:@"price"];
            };*/
            buy.str_money=[dic objectForKey:@"price"];
            
            buy.m_b_is_have_say = [[dic objectForKey:@"is_commented"] boolValue];
            buy.end_time = self.end_time;
            //NSLog(@"%@,%@",buy.stringid,buy.stringurl);
            
            //share
            //buy.str_share_img_url = [NSString stringWithFormat:@"%@%@",JPG, [dic objectForKey:@"photo"]];
            buy.str_share_name = [[dic objectForKey:@"goods"] objectForKey:@"name"];
            FIGoodsDetailsCell * cell = (FIGoodsDetailsCell *)[collectionView cellForItemAtIndexPath:indexPath];
            if (cell) {
                buy.image_share_ = cell.image_goods.image;
            }
            
            [self.navigationController pushViewController:buy animated:YES];
        }
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    if (self.list_goods) {
        return [self.list_goods count];
    }
    return 0;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    FIGoodsDetailsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FIGoodsDetailsCell"
                                                                         forIndexPath:indexPath];
    cell.tag=indexPath.row;
    cell.btn_put_maseter.tag = indexPath.row;
    [cell.btn_put_maseter addTarget:self action:@selector(doActionPut:) forControlEvents:UIControlEventTouchUpInside];
    
    if (!self.m_b_is_notice) {
        cell.btn_join.enabled=YES;
        cell.label_join.text=@"立即参团";
        cell.label_join.textColor=[UIColor whiteColor];
    } else {
        cell.btn_join.enabled=NO;
        cell.label_join.text=@"暂未开团";
        cell.label_join.textColor=[UIColor grayColor];
    }
//    if (indexPath.row%2==0) {
//        cell.backgroundColor=[UIColor redColor];
//    } else {
//        cell.backgroundColor=[UIColor grayColor];
//    }
    
    // load the image for this cell
    NSInteger index = indexPath.row;
    if (index < [self.list_goods count]) {
        NSDictionary * dic = [self.list_goods objectAtIndex:index];
        if (dic && [dic isKindOfClass:[NSDictionary class]]) {
            NSDictionary * dic_goods = [dic objectForKey:@"goods"];
            if (dic_goods && [dic_goods isKindOfClass:[NSDictionary class]]) {
                //img
                NSString * photo = [dic_goods objectForKey:@"photo"];
                if (photo && [photo isKindOfClass:[NSString class]]) {//goods
                    NSString * str_url = [NSString stringWithFormat:@"%@%@", JPG, photo];
                    [cell.image_goods setImageWithURL:[NSURL URLWithString:str_url] placeholderImage:[UIImage imageNamed:@"deimagegroupdeloading"]];
                }
                //title
                NSString * name = [dic_goods objectForKey:@"name"];
                if (name && [name isKindOfClass:[NSString class]]) {
                    cell.label_title.text = name;
                }
                //money
                NSString * price_real = [dic_goods objectForKey:@"price"];
                if (price_real && [price_real isKindOfClass:[NSString class]]) {
                    NSString * str_price = [NSString stringWithFormat:@"¥%@", price_real];
                    cell.label_money_fail.text=str_price;
                }
            }
            NSString * price_fail = [dic objectForKey:@"price"];
            if (price_fail && [price_fail isKindOfClass:[NSString class]]) {
                NSString * str_price = [NSString stringWithFormat:@"¥%@", price_fail];
                cell.label_money_real.text=str_price;
            }
            if (!self.m_b_is_notice) {
                //添加售罄逻辑
                NSInteger soldout = [[dic objectForKey:@"soldout"] integerValue];
                if (soldout!=0) {
                    cell.btn_join.enabled=NO;
                    cell.label_join.text=@"售罄";
                    cell.label_join.textColor=[UIColor grayColor];
                }
            }
        }
        //title
    }
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    FIGoodsDetailsHeader *header = nil;
    if([kind isEqual:UICollectionElementKindSectionHeader]){
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"FIGoodsDetailsHeader"
                                                           forIndexPath:indexPath];
        CGFloat width = self.view.frame.size.width;
        //标题图片
        if (self.list_imgs) {
            NSInteger count = [self.list_imgs count];
            if (count < 3) {
                count=3;
            }
            
            for (int i = 0; i < count; i++) {
                UIImageView * imgvv = [[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, 164)];
                imgvv.contentMode=UIViewContentModeScaleAspectFill;
                [header.scrollview_base addSubview:imgvv];
                
                if (i < [self.list_imgs count]) {
                    NSDictionary * dic = [self.list_imgs objectAtIndex:i];
                    if (dic && [dic isKindOfClass:[NSDictionary class]]) {
                        NSString * str_url = [dic objectForKey:@"img"];
                        if (str_url && [str_url isKindOfClass:[NSString class]]) {
                            NSString * str_bb = [NSString stringWithFormat:@"%@%@", JPG, str_url];
                            if (str_bb) {
                                [imgvv setImageWithURL:[NSURL URLWithString:str_bb] placeholderImage:[UIImage imageNamed:@"textbg1"]];
                            }
                        }
                    }
                } else {
                    [imgvv setImage:[UIImage imageNamed:@"textbg1"]];
                }
                
                if (i==0) {
                    image_share_0 = imgvv;
                }
            }
            header.scrollview_base.contentSize=CGSizeMake(count*width, 164);
            //点
            header.page_control.numberOfPages=count;
            header.page_control.currentPage=0;
        }
        //商品描述
        if (self.data_net_des) {
            header.label_title.text=self.data_net_des;
        } else {
            header.label_title.text=@"没有标题描述";
        }
    }
    return header;
}

@end
























